/*
 * Copyright 2015 Yan Zhenjie
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yanzhenjie.nohttp.sample.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.yanzhenjie.nohttp.Logger;
import com.yanzhenjie.nohttp.NoHttp;
import com.yanzhenjie.nohttp.rest.Request;
import com.yanzhenjie.nohttp.rest.RequestQueue;
import com.yanzhenjie.nohttp.sample.R;
import com.yanzhenjie.nohttp.sample.dialog.ImageDialog;
import com.yanzhenjie.nohttp.sample.nohttp.HttpListener;
import com.yanzhenjie.nohttp.sample.nohttp.HttpResponseListener;

import butterknife.ButterKnife;

/**
 * Created in 2016/5/8 18:19.
 *
 * @author Yan Zhenjie.
 */
public abstract class BaseActivity extends AppCompatActivity
{

	private CoordinatorLayout mCoordinatorLayout;

	private AppBarLayout mAppBarLayout;

	private Toolbar mToolbar;

	private FrameLayout mContentLayout;

	@Override
	protected final void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		Logger.e(getClass().getName());
		getDelegate().setContentView(R.layout.activity_base);

		// 初始化请求队列，传入的参数是请求并发值。
		mQueue = NoHttp.newRequestQueue(1);

		mCoordinatorLayout = ButterKnife.findById(this, R.id.coordinatorlayout);
		mAppBarLayout = ButterKnife.findById(this, R.id.appbarlayout);
		mToolbar = ButterKnife.findById(this, R.id.toolbar);
		mContentLayout = ButterKnife.findById(this, R.id.content);

		setSupportActionBar(mToolbar);
		setBackBar(true);
		onActivityCreate(savedInstanceState);
	}

	//-------------- NoHttp -----------//

	/**
	 * 用来标记取消。
	 */
	private Object object = new Object();

	/**
	 * 请求队列。
	 */
	private RequestQueue mQueue;

	/**
	 * 发起请求。
	 *
	 * @param what      what.
	 * @param request   请求对象。
	 * @param callback  回调函数。
	 * @param canCancel 是否能被用户取消。
	 * @param isLoading 实现显示加载框。
	 * @param <T>       想请求到的数据类型。
	 */
	public <T> void request(int what, Request<T> request, HttpListener<T> callback, boolean canCancel, boolean
			isLoading)
	{
		request.setCancelSign(object);
		mQueue.add(what, request, new HttpResponseListener<>(this, request, callback, canCancel, isLoading));
	}

	@Override
	protected void onDestroy()
	{
		// 和声明周期绑定，退出时取消这个队列中的所有请求，当然可以在你想取消的时候取消也可以，不一定和声明周期绑定。
		mQueue.cancelBySign(object);

		// 因为回调函数持有了activity，所以退出activity时请停止队列。
		mQueue.stop();

		super.onDestroy();
	}

	protected void cancelAll()
	{
		mQueue.cancelAll();
	}

	protected void cancelBySign(Object object)
	{
		mQueue.cancelBySign(object);
	}

	// -------------------- BaseActivity的辅助封装 --------------------- //

	protected abstract void onActivityCreate(Bundle savedInstanceState);

	public CoordinatorLayout getCoordinatorLayout()
	{
		return mCoordinatorLayout;
	}

	public FrameLayout getContentLayout()
	{
		return mContentLayout;
	}

	public AppBarLayout getAppBarLayout()
	{
		return mAppBarLayout;
	}

	public Toolbar getmToolbar()
	{
		return mToolbar;
	}

	@Override
	public void setTitle(CharSequence title)
	{
		mToolbar.setTitle(title);
	}

	@Override
	public void setTitle(int titleId)
	{
		mToolbar.setTitle(titleId);
	}

	public void setSubtitle(CharSequence title)
	{
		mToolbar.setSubtitle(title);
	}

	public void setSubtitle(int titleId)
	{
		mToolbar.setSubtitle(titleId);
	}

	public void setSubtitleTextColor(int color)
	{
		mToolbar.setSubtitleTextColor(color);
	}

	public void setTitleTextColor(int color)
	{
		mToolbar.setTitleTextColor(color);
	}

	public void setBackBar(boolean isShow)
	{
		getSupportActionBar().setDisplayHomeAsUpEnabled(isShow);
	}

	public void setContentBackground(int color)
	{
		mContentLayout.setBackgroundResource(color);
	}

	@Override
	public void setContentView(int layoutResID)
	{
		mContentLayout.removeAllViews();
		getLayoutInflater().inflate(layoutResID, mContentLayout, true);
	}

	@Override
	public void setContentView(View view)
	{
		mContentLayout.removeAllViews();
		mContentLayout.addView(view);
	}

	@Override
	public void setContentView(View view, ViewGroup.LayoutParams params)
	{
		mContentLayout.addView(view, params);
	}

	@Override
	public final boolean onOptionsItemSelected(MenuItem item)
	{
		if (item.getItemId() == android.R.id.home)
		{
			finish();
			return true;
		}
		return onOptionsItemSelectedCompat(item);
	}

	protected boolean onOptionsItemSelectedCompat(MenuItem item)
	{
		return false;
	}

	public ViewGroup getContentRoot()
	{
		return mContentLayout;
	}

	/**
	 * Show message dialog.
	 *
	 * @param title   title.
	 * @param message message.
	 */
	public void showMessageDialog(int title, int message)
	{
		showMessageDialog(getText(title), getText(message));
	}

	/**
	 * Show message dialog.
	 *
	 * @param title   title.
	 * @param message message.
	 */
	public void showMessageDialog(int title, CharSequence message)
	{
		showMessageDialog(getText(title), message);
	}

	/**
	 * Show message dialog.
	 *
	 * @param title   title.
	 * @param message message.
	 */
	public void showMessageDialog(CharSequence title, int message)
	{
		showMessageDialog(title, getText(message));
	}

	/**
	 * Show message dialog.
	 *
	 * @param title   title.
	 * @param message message.
	 */
	public void showMessageDialog(CharSequence title, CharSequence message)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle(title);
		builder.setMessage(message);
		builder.setPositiveButton(R.string.know, (dialog, which) -> dialog.dismiss());
		builder.show();
	}

	/**
	 * 显示图片dialog。
	 *
	 * @param title  标题。
	 * @param bitmap 图片。
	 */
	public void showImageDialog(CharSequence title, Bitmap bitmap)
	{
		ImageDialog imageDialog = new ImageDialog(this);
		imageDialog.setTitle(title);
		imageDialog.setImage(bitmap);
		imageDialog.show();
	}

}
